package la.iok.hzsvn.lewin.movie.controller;

import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.core.movieservice.param.NameSequenceParam;
import la.iok.hzsvn.lewin.movie.model.*;
import la.iok.hzsvn.lewin.movie.service.BookChapterService;
import la.iok.hzsvn.lewin.movie.service.PlayRoleBookService;
import la.iok.hzsvn.share.response.ListDataResponse;
import la.iok.hzsvn.share.response.OperatorResponse;
import la.iok.hzsvn.share.response.OperatorResponses;
import la.iok.hzsvn.share.response.SingleDataResponse;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("chapters")
public class ChapterController {
    private final BookChapterService bookChapterService;
    private final PlayRoleBookService playRoleBookService;

    public ChapterController(BookChapterService bookChapterService,
                             PlayRoleBookService playRoleBookService) {
        this.bookChapterService = bookChapterService;
        this.playRoleBookService = playRoleBookService;
    }

    /**
     * 保存章节
     * @param token 操作用户
     * @param id 剧本id
     * @param param 章节信息
     * @return 操作结果，包含保存章节的id
     */
    @PostMapping("movie/{id}")
    public SingleDataResponse<Long> save(@RequestHeader Token token, @PathVariable Long id, @RequestBody NameSequenceParam param){
        return new SingleDataResponse<>(bookChapterService.save(token,id,param));
    }

    /**
     * 修改章节信息
     * @param token 操作用户
     * @param id 章节id
     * @param param 章节信息
     * @return 操作结果
     */
    @PutMapping("{id}")
    public OperatorResponse update(@RequestHeader Token token, @PathVariable Long id, @RequestBody NameSequenceParam param) {
        bookChapterService.update(token,id,param);
        return OperatorResponses.success();
    }

    /**
     * 删除章节
     * @param token 操作用户
     * @param id 章节id
     * @return 操作结果
     */
    @DeleteMapping("{id}")
    public OperatorResponse delete(@RequestHeader Token token, @PathVariable Long id){
        playRoleBookService.deleteChapter(token,id);
        return OperatorResponses.success();
    }

    /**
     * 查询剧本的所有章节,以sequence升序排列
     * @param token 操作用户
     * @param movieId 剧本id
     * @return 章节列表
     */
    @GetMapping("movie/{movieId}")
    public ListDataResponse<BookChapterVo> chapters(@RequestHeader Token token, @PathVariable Long movieId){
        return new ListDataResponse<>(bookChapterService.movieChapters(token,movieId));
    }
}
